package com.satyam.smooth.domain;

import java.io.Serializable;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

@Entity
public class PurchaseReturn implements Serializable {

    private static final long serialVersionUID = 1L;

    public PurchaseReturn() {
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToOne(optional = false)
    private Purchase purchase;

    @Column(nullable = false)
    private Date onDate;

    @ManyToOne(optional = false)
    private User user;

    @Column(nullable = false)
    private BigDecimal vat = BigDecimal.ZERO;

    @Column(nullable = false)
    private BigDecimal total = BigDecimal.ZERO;

    @Column(nullable = false)
    private BigDecimal grandTotal = BigDecimal.ZERO;

    @OneToMany(mappedBy = "purchaseReturn", fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST,
        CascadeType.MERGE, CascadeType.REMOVE})
    private Set<PurchaseReturnDetail> purchaseReturnDetails = new HashSet<>();

    private String remark;

    public void addPurchaseReturnDetail(PurchaseReturnDetail purchaseReturnDetail) {
        purchaseReturnDetail.setPurchaseReturn(this);
        getPurchaseReturnDetails().add(purchaseReturnDetail);
    }

    public void calculateGrandTotal() {
        for (PurchaseReturnDetail prd : getPurchaseReturnDetails()) {
            this.total.add(prd.getTotal());
        }

        //grandTotal = grandTotal + (1 + vat/100);
        this.grandTotal.add(this.total);
        this.grandTotal.multiply(new BigDecimal(BigInteger.ONE).add(this.vat.divide(new BigDecimal(100))));
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Purchase getPurchase() {
        return purchase;
    }

    public void setPurchase(Purchase purchase) {
        this.purchase = purchase;
    }

    public Date getOnDate() {
        return onDate;
    }

    public void setOnDate(Date onDate) {
        this.onDate = onDate;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public BigDecimal getVat() {
        return vat;
    }

    public void setVat(BigDecimal vat) {
        this.vat = vat;
    }

    public BigDecimal getTotal() {
        return total;
    }

    public void setTotal(BigDecimal total) {
        this.total = total;
    }

    public BigDecimal getGrandTotal() {
        return grandTotal;
    }

    public void setGrandTotal(BigDecimal grandTotal) {
        this.grandTotal = grandTotal;
    }

    public Set<PurchaseReturnDetail> getPurchaseReturnDetails() {
        return purchaseReturnDetails;
    }

    public void setPurchaseReturnDetails(Set<PurchaseReturnDetail> purchaseReturnDetails) {
        this.purchaseReturnDetails = purchaseReturnDetails;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
    }

}
